var e = getApp();
const {
  getCodeList,
  createOrder,
  getOpenid
} = require('../../common/api/user')
const api = require('../../common/utils/request.js')

Component({
  properties: {
    show: {
      value: false,
      type: Boolean
    },
    openid: {
      value: '',
      type: String
    }
  },

  data: {
    list: [],
    current: 0,
    price: 0,
    taocan_id: 0,
    payTypeList: [{
        title: '金币支付',
        icon: '../../common/static/pay-jinbi-icon.png',
        type: 2,
      },
      {
        title: '微信支付',
        icon: '../../common/static/pay-wechat-icon.png',
        type: 1,
      }
    ],
    currentPay: 1,
    pay_type: 1,
    isLoading: false
  },
  ready: function () {
    var _this = this;
    api.post(getCodeList).then(res => {
      //成功时回调函数
      console.log(res.errno, 'getCodeList')
      if (res.errno == 0) {
        _this.setData({
          list: res.data,
          price: res.data[0].price,
          taocan_id: res.data[0].id
        })
      }
    }).catch(err => {
      //失败时回调函数
      console.log(err)
    })

  },
  methods: {
    changePayType(e) {
      var item = e.currentTarget.dataset.item;
      var index = e.currentTarget.dataset.index;
      console.log(e.currentTarget, 'changePayTypechangePayType')
      this.setData({
        currentPay: index,
        pay_type: item.type
      })
    },
    buyNow() {
      if(this.data.isLoading){
        wx.showToast({
          title: '请勿重复点击',
          icon: 'none'
        })
        return 
      }
      wx.showLoading({
        title: '加载中',
      })
      this.setData({
        isLoading: true
      })
      var _this = this;
      api.post(createOrder, {
        pt_id: e.globalData.ptId,
        uid: wx.getStorageSync('yz_uid'),
        type: 3, // 1充值积分2充值金币3购买套餐
        taocan_id: this.data.taocan_id,
        pay_type: this.data.pay_type, //1微信支付2金币支付 （只有买套餐有金币支付）
        openid: this.data.openid
      }).then(res => {
        //成功时回调函数
        console.log(res.errno, 'getCodeList')
        if (res.errno == 0) {
          var payInfo = res.data.payInfo;
          if(this.data.pay_type == 2){
            wx.showToast({
              title: '购买成功',
              icon: 'none'
            })
            this.setData({
              isLoading: false
            })
            wx.hideLoading();
            _this.triggerEvent("confirm");
            _this.setData({
              show: false
            });
          }else {
            this.wechatPay(payInfo)
          }
        }else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
          this.setData({
            isLoading: false
          })
          wx.hideLoading();
        }
        // var payInfo = {
        //   appId: "wx3faa28fc6e94b17a",
        //   timestamp: "1754981691",
        //   nonceStr: "0ta2iapnv6w3kj51d2sqvmjxnxwe4q0e",
        //   package: "prepay_id=wx121454516049247774a73990d1abb40000",
        //   signType: "MD5",
        //   paySign: "8720FAA04BCC526F6C58E775B16DAF70",
        //   timestamp: "1754981691"
        // };
        // this.wechatPay(payInfo)
      }).catch(err => {
        //失败时回调函数
        console.log(err)
        this.setData({
          isLoading: false
        })
        wx.hideLoading();
      })
    },
    wechatPay(info) {
      var t = this;
      wx.requestPayment({
        timeStamp: info.timestamp,
        nonceStr: info.nonceStr,
        package: info.package,
        signType: info.signType ? info.signType : "MD5",
        paySign: info.paySign,
        success: function (a) {
          wx.showModal({
            title: "提示",
            content: "支付成功",
            showCancel: !1,
            success: function (a) {
              t.setData({
                isLoading: false
              })
              wx.hideLoading();
              if (a.confirm) {
                t.triggerEvent("confirm");
                t.setData({
                  show: false
                });
                // wx.navigateTo({
                //   url: "/haoduogeni/mine/index"
                // })
              }
            }
          });
        },
        fail: function (a) {
          console.log(a), wx.showToast({
            title: "支付失败请稍后重试",
            icon: "none",
            duration: 2e3
          });
          t.setData({
            isLoading: false
          })
          wx.hideLoading();
        }
      });
    },
    changeTaocan(e) {
      console.log(e.currentTarget.dataset.index, 'eeeeeeeeeeeee')
      var index = e.currentTarget.dataset.index;
      var item = e.currentTarget.dataset.item;
      this.setData({
        current: index,
        price: item.price,
        taocan_id: item.id
      })
    },
    popupSpecsbtn: function (t) {
      console.log(t);
      this.triggerEvent("closeShow");
      this.setData({
        show: false
      });
    },
  }
});